<?php
namespace app\api\service;
use think\Controller;
use think\Db;

class MemberCar extends Controller {
    static protected $err_code = 0;
    //获取我的爱车列表
    static public function get_my_car($param) {
        if(!isset($param['member_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try {
            $my_car = db('my_car')->alias('c')->field('c.id, c.name, c.is_default, c.brand_id, c.model_id, c.mileage, b.image')
                ->join('brand b', 'b.id = c.brand_id')
                ->where('c.member_id', $param['member_id'])->order('c.is_default desc')->select();
            return ['code' => 200, 'data' => $my_car, 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code' => self::$err_code, 'msg' => 'FAULT'];
        }
    }

    // 获取我的爱车详情
    static public function get_my_car_info($param) {
        if(!isset($param['member_id']) || !isset($param['car_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try {
            $my_car = db('my_car')->where('member_id', $param['member_id'])->where('id', $param['car_id'])->find();
            return ['code' => 200, 'data' => $my_car, 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code' => self::$err_code, 'msg' => 'FAULT'];
        }
    }

    // 保存我的爱车
    static public function save_my_car($param) {
        if(!isset($param['member_id']) || !isset($param['id']) ||!isset($param['brand_id']) || !isset($param['model_id']) ||
            !isset($param['mileage']) || !isset($param['displacement']) || !isset($param['name']) ||
            !isset($param['plate']) || !isset($param['location']) ||
            !isset($param['production_time']) || !isset($param['start_off_time']))
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        // 启动事务
        Db::startTrans();
        try {
            $data = [
                'brand_id' => $param['brand_id'],
                'model_id' => $param['model_id'],
                'mileage' => $param['mileage'],
                'name' => $param['name'],
                'displacement' => $param['displacement'],
                'plate' => $param['plate'],
                'location' => $param['location'],
                'production_time' => $param['production_time'],
                'start_off_time' => $param['start_off_time'],
                'add_time' => time()
            ];

            $image = '';
            if ($param['id'] > 0) {
                if (isset($param['image']) && !empty($param['image'])) {
                    $data['image'] = $param['image'];
                    // 获取原图
                    $image = Db::table('mob_my_car')->where('id', $param['id'])->where('member_id', $param['member_id'])->value('image');
                }
                Db::table('mob_my_car')->where('id', $param['id'])->where('member_id', $param['member_id'])->update($data);
            } else {
                $data['image'] = $param['image'];
                $data['member_id'] = $param['member_id'];
                Db::table('mob_my_car')->insert($data);
            }
            // 提交事务
            Db::commit();

            if ($image) unlink('.' . $image);  // 如果有原图  删除

            return ['code' => 200, 'msg' => 'SUCCESS'];
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }

    // 设置默认车辆
    static public function save_default_car($param) {
        if(!isset($param['member_id']) || !isset($param['id']) ||!isset($param['is_default']))
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        // 启动事务
        Db::startTrans();
        try {
            if ($param['is_default'] == 1) {
                // 将用户其他车辆默认状态设置为0
                Db::table('mob_my_car')->where('member_id', $param['member_id'])->update(['is_default' => 0]);
                // 将选中车辆设为默认
                Db::table('mob_my_car')->where('id', $param['id'])->where('member_id', $param['member_id'])->update(['is_default' => 1]);
            } else {
                Db::table('mob_my_car')->where('id', $param['id'])->where('member_id', $param['member_id'])->update(['is_default' => 0]);
            }
            // 提交事务
            Db::commit();
            return ['code' => 200, 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }
}





